import json


class CCfg:
    def __init__(self, filepath='config.json'):
        self.filepath = filepath
        self.file_content = {}  # 模拟文件内容
        self.printFunc = print

        self.__all_people = ""
        self.__scheduled_people = ""
        self.__actual_participants = ""

        self.load_configuration()

    def load_configuration(self):
        try:
            with open(self.filepath, 'r') as f:
                config_data = json.load(f)
                self.__all_people = config_data.get('all_people', '')
                self.__scheduled_people = config_data.get('scheduled_people', '')
                self.__actual_participants = config_data.get('actual_participants', '')
        except FileNotFoundError:
            # 文件不存在时，保持当前默认属性值，也可以在这里做一些初始化文件的操作等
            pass
        except json.JSONDecodeError:
            print(f"配置文件 {self.filepath} 格式有误，请检查！")

    def SetPrintFunc(self, func):
        self.printFunc = func
        return

    def print(self, s, end="\r\n"):
        self.printFunc(s, end)

    def save_configuration(self):
        config_data = {
            "all_people": self.__all_people,
            "scheduled_people": self.__scheduled_people,
            "actual_participants": self.__actual_participants
        }
        try:
            with open(self.filepath, 'w') as f:
                json.dump(config_data, f)
        except IOError:
            print(f"保存配置文件 {self.filepath} 时出错，请检查权限等问题！")

    def SetAllPeople(self, data):
        self.__all_people = data
        self.save_configuration()

    def SetScheduledPeople(self, data):
        self.__scheduled_people = data
        self.save_configuration()

    def SetActualParticipants(self, data):
        self.__actual_participants = data
        self.save_configuration()

    def DisplayConfiguration(self):
        self.print("全部人:", self.GetAllPeople())
        self.print("被安排的人:", self.GetScheduledPeople())
        self.print("实际的人:", self.GetActualParticipants())

    def GetAllPeople(self):
        self.load_configuration()
        return self.__all_people

    def GetScheduledPeople(self):
        self.load_configuration()
        return self.__scheduled_people

    def GetActualParticipants(self):
        self.load_configuration()
        return self.__actual_participants
